#include "Store_menu.h"
#include <algorithm>
#include "Graph.h"
#include "Window.h"
using namespace std;
using namespace Store;

GraphingVCN::GraphingVCN( ) { };

extern Store_menu m;

bool CustTotCompare(CustTotal a, CustTotal b){
        return a.getTot() > b.getTot();
}

vector<CustTotal> GraphingVCN::graph_display(){
	
	vector<Cart>carts = m.get_carts();
	vector<Product>prods=m.get_prods();
	vector<Cart_item>cart_items=m.get_cart_items();
	vector<Customer>custs=m.get_custs();
	vector<double>total(12);
	double sum=0;
	
	int cart_id, prod_id, cust_id(-1);
	double prod_qty, prod_price;
	vector<CustTotal> cust_totals;
	for(int i=0; i<custs.size();i++)
	{
		cust_id=custs[i].getCustId();
		for (int j=0; j<carts.size();j++)
		{
			if (carts[j].getCustId()==cust_id) //looks for customer id in carts vector
			{
				cart_id=carts[j].getCartId(); //found the cart id for a customer
		
				for(int k=0; k<cart_items.size(); k++)
				{
					if (cart_items[k].getCartId()==cart_id) //looks for cart id in the cart_item vector
					{
						prod_id=cart_items[k].getProdId(); //found Prod id for the cart id
						prod_qty=cart_items[k].getProdQty(); //found the Prod qty for cart id
					
						for (int m=0; m<prods.size(); m++)
						{
							if (prods[m].getProdId()==prod_id) //looks for product in prods vector
							{
								prod_price=prods[m].getProdPrice(); //found the product price
								sum+=prod_price*prod_qty;
								
							}}}}}}
		cust_totals.push_back(CustTotal(custs[i],sum));
		sum=0;
	}
		
	sort(cust_totals.begin(),cust_totals.end(),CustTotCompare);
	
	
	return cust_totals;
}
		
	
